/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mediatek.settings.deviceinfo;

import android.content.Context;
import android.os.SystemProperties;
import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.mediatek.settings.FeatureOption;
//huanghb add
import android.util.Log;
import android.text.TextUtils;
import android.content.Intent;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class CustomizeBuildVersionPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin {

    private static final String KEY_CUSTOM_BUILD_VERSION = "custom_build_version";
    private static final String CUSTOM_BUILD_VERSION_PROPERTY
            = "ro.vendor.mediatek.version.release";
    private int i = 0;//huanghb add

    public CustomizeBuildVersionPreferenceController(Context context) {
        super(context);
    }

    @Override
    public String getPreferenceKey() {
        return KEY_CUSTOM_BUILD_VERSION;
    }

    @Override
    public boolean isAvailable() {
        return true;
    }
    //huanghb add
    
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
            return false;
        }
        i++;
        Log.d("huanghb","handlePreferenceTreeClick i = "+i);
        if(i == 10){
            addShutDownDialog();
            i = 0;
        }
        
        return true;
    }
    
    private AlertDialog dialog;

    public void addShutDownDialog() {
        final String[] items = {mContext.getString(R.string.fise_dialog_factory_title), mContext.getString(R.string.fise_dialog_aging_title)};
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(mContext.getString(R.string.fise_dialog_message_title));
        //builder.setMessage(mContext.getString(R.string.fise_dialog_message));
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("dddd","which = "+which);
                sendFactoryTestActicity(which);
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dia, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        //dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }
    
    private void sendFactoryTestActicity(int i){
        Log.d("huanghb","sendFactoryTestActicity");
        Intent intent = new Intent();
        //intent.setClassName("com.android.dialer","com.android.dialer.main.impl.MainActivity");
        if(i == 0){
            intent.setClassName("com.android.ipro", "com.android.ipro.FactoryTestActivity");
        }else if(i == 1){
            intent.setClassName("com.android.systemui", "com.android.systemui.statusbar.phone.PlayLocalVideoActivity");
            intent.putExtra("video_path", "/system/media/SPEC.mp4");
        }
        if(intent==null){
        }else{
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        }
    }
    //huanghb end

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        preference.setSummary(SystemProperties.get(CUSTOM_BUILD_VERSION_PROPERTY,
                mContext.getResources().getString(R.string.device_info_default)));
    }

}
